package com.lanchetech.user.service.impl;


import com.alibaba.excel.util.CollectionUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lanchetech.bean.request.UserVoucherReq;
import com.lanchetech.bean.response.BasePageResp;
import com.lanchetech.bean.response.BaseResp;
import com.lanchetech.bean.response.ResultData;
import com.lanchetech.bean.util.PageHelpUtil;
import com.lanchetech.bean.vo.UserVoucherVO;
import com.lanchetech.common.enums.ResultCode;
import com.lanchetech.common.enums.ToggleEnum;
import com.lanchetech.common.enums.UserStaffTypeEnum;
import com.lanchetech.common.enums.VoucherTypeEnum;
import com.lanchetech.dao.SpuMapper;
import com.lanchetech.dao.UserMapper;
import com.lanchetech.dao.UserVoucherMapper;
import com.lanchetech.entity.Shop;
import com.lanchetech.entity.Spu;
import com.lanchetech.entity.User;
import com.lanchetech.entity.UserVoucher;
import com.lanchetech.service.DaoService;
import com.lanchetech.user.service.OperatorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class OperatorServiceImpl implements OperatorService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    SpuMapper spuMapper;

    @Autowired
    UserVoucherMapper userVoucherMapper;

    @Autowired
    DaoService daoService;

    @Override
    public BaseResp sendUserVoucher(User user, String username, Long spuId) {

        if (UserStaffTypeEnum.STAFF.getType().equals(user.getStaffType())) {
            // 仅限员工操作
            User toUser = userMapper.findOneByUsername(username);
            if (toUser == null) {
                return new BaseResp(ResultCode.ACCT_NOT_EXIST);
            }

            Spu spu = spuMapper.selectByPrimaryKey(spuId);
            if (spu == null) {
                return new BaseResp(ResultCode.SPU_EXCEPTION);
            }

            userVoucherMapper.insert(UserVoucher.builder()
                    .userId(toUser.getId())
                    .productName(spu.getName())
                    .shopId(spu.getShopId())
                    .spuId(spu.getId())
                    .type(VoucherTypeEnum.GIFT.getType())
                    .status(ToggleEnum.OFF.getStatus())
                    .createdAt(new Date())
                    .updatedAt(new Date())
                    .build());
            return new BaseResp();
        }

        return new BaseResp(ResultCode.FAIL);
    }

    @Override
    public ResultData<BasePageResp<UserVoucherVO>> getGiftUserVoucherPage(User user, UserVoucherReq req) {
        if (UserStaffTypeEnum.STAFF.getType().equals(user.getStaffType())) {
            req.setType(VoucherTypeEnum.GIFT.getType());

            Page page = PageHelper.startPage(req.getPageNo(), req.getPageSize());
            List<UserVoucherVO> list = userVoucherMapper.findAllPage(req);
            if (!CollectionUtils.isEmpty(list)) {
                Map<Long, User> userMap = daoService.getUserMap(list.stream().map(UserVoucherVO::getUserId).collect(Collectors.toList()));
                Map<Long, Shop> shopMap = daoService.getShopMap(list.stream().map(UserVoucherVO::getShopId).collect(Collectors.toList()));
                Map<Long, Spu> spuMap = daoService.getSpuMap(list.stream().map(UserVoucherVO::getSpuId).collect(Collectors.toList()));
                list.stream().forEach(item -> {
                    item.setUserNickname(userMap.get(item.getUserId()).getNickname());
                    item.setAvatar(userMap.get(item.getUserId()).getAvatar());
                    item.setShopName(shopMap.get(item.getShopId()).getShopName());
                    item.setSpuImage(spuMap.get(item.getSpuId()).getMainImage());
                });
            }
            return PageHelpUtil.buildPage(list, page);
        }
        return null;
    }
}